package com.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 * Servlet implementation class AdminLoginServlet
 */
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
private final static String DATASOURCE_NAME = "jdbc/FlyGreenwichAirline";
	
	private DataSource datasource;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
		int admin_id = 0;
		
		String myusername = request.getParameter("myusername");
		String mypassword = request.getParameter("mypassword");
		
		java.sql.Connection con=null;
		
		System.out.println("Username: "+myusername);
		System.out.println("Password: "+mypassword);

    	String query = "SELECT id from admin WHERE username = '"+ myusername +"' AND password = '"+ mypassword +"';";

						
    	try {
      		
    		Context env = (Context) new InitialContext().lookup("java:comp/env");
			datasource = (DataSource) env.lookup(DATASOURCE_NAME);
    		con = datasource.getConnection();

            java.sql.Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery (query);
            
            while(rs.next()){
            admin_id= rs.getInt(1);
                        
            System.out.println("admin_id: "+admin_id);
            }
               	
            rs.close();
            stmt.close();
            
        }  // end try

    catch (SQLException ex) {
            
		while (ex != null) {  
				ex.printStackTrace();
              }  // end while

        }  // end catch SQLException

   catch (java.lang.Exception ex) {
      	ex.printStackTrace();
	  }
    	
    	finally{
    		try{
    			if (con != null) con.close();
    		}catch(SQLException ignored){
    		ignored.printStackTrace();
    		}
    	}
    	
    	request.setAttribute("admin_id", admin_id);
    	session.setAttribute("admin_id", admin_id);

    	String nextJSP = "/admin/index.jsp";
    	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    	dispatcher.forward(request,response);
	}


}
